#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
typedef pair<int,int> PII;
const int INF=0x3f3f3f3f;
int add(int n)
{
	if(n==10)
	{
		return 0;
	}
	if(n<=110)
	{
		int ans1=1,ans2=0;
		int num=110;
		int n1=num-n;
		ans1+=min((n1)/10+(n1-(n1)/10*10),(n1)/10+1+((n1)/10+1)*10-(n1));
		n1=n-10;
		ans2=min((n1)/10+(n1-(n1)/10*10),(n1)/10+1+((n1)/10+1)*10-(n1));
		return min(ans1,ans2);
	}
	if(n>110 && n<=200)
	{
		int ans1=1;
		int ans2=2;
		int n1=n-110;
		ans1+=min((n1)/10+(n1-(n1)/10*10),(n1)/10+1+((n1)/10+1)*10-(n1));
		n1=200-n;
		ans2+=min((n1)/10+(n1-(n1)/10*10),(n1)/10+1+((n1)/10+1)*10-(n1));
		return min(ans1,ans2);	
	}
	if(n>200 && n<=210)
	{
		int ans1=2,ans2=2;
		int n1=n-200;
		ans1+=min((n1)/10+(n1-(n1)/10*10),(n1)/10+1+((n1)/10+1)*10-(n1));
		n1=210-n;
		ans2+=min((n1)/10+(n1-(n1)/10*10),(n1)/10+1+((n1)/10+1)*10-(n1));
		return min(ans1,ans2);	
	}
	if(n>210)
	{
		int ans1=2;
		int ans2=1;
		int n1=n-210;
		ans1+=min((n1)/10+(n1-(n1)/10*10),(n1)/10+1+((n1)/10+1)*10-(n1));
		n1=300-n;
		ans2+=min((n1)/10+(n1-(n1)/10*10),(n1)/10+1+((n1)/10+1)*10-(n1));
		return min(ans1,ans2);
	}
	
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		
		int a;
		int ans=0;
		for(int i=0;i<4;i++)
		{
			cin>>a;
			ans+=add(a);
		}
		cout<<ans<<endl;
	}

	return 0;
}

